package controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UrlUtility;

import model.FacilityDTO;
import model.PriorityDTO;
import model.PublicHolidayDTO;

import bizlogic.FacilityManager;
import bizlogic.PriorityManager;

@WebServlet("/ProcessFacility")
public class ProcessFacilityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProcessFacilityServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doShow(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	private void doShow(HttpServletRequest req, HttpServletResponse res) {
		String address = "/WEB-INF/ManageFacility.jsp";
		String action = req.getParameter("action");
		
		FacilityManager m = null;
		FacilityDTO data = new FacilityDTO();
					
			if (action.trim().equalsIgnoreCase("insert")) {
				m = new FacilityManager();
				req.setAttribute("action", "insert");
				req.setAttribute("data", data);
				
				UrlUtility.forwardPage(req, res, address);	
				
							
			} else if(action.equalsIgnoreCase("edit")) {
				
				req.setAttribute("action", "edit");
				m = new FacilityManager();
				String name = req.getParameter("name");
				 data = m.getFacility(name);
				req.setAttribute("facilities", data);
			
				UrlUtility.forwardPage(req, res, address);	
			}
			else
			{
				UrlUtility.redirectPage(res, "LoadFacility");
			}
			
	}
	private void processRequest(HttpServletRequest request, HttpServletResponse response) {
		FacilityManager fm = new FacilityManager();
		
		String action = "", error = "";
		boolean valid = true;
		
		// Validation
		FacilityDTO facility = new FacilityDTO();
		
		try {
			facility.setName(request.getParameter("name"));
			facility.setDescription(request.getParameter("description"));
			facility.setType(request.getParameter("type"));
			facility.setSeatingCapacity(Integer.parseInt(request.getParameter("seatingCapacity")));
			System.out.println(request.getParameter("status"));
			facility.setStatus(Integer.parseInt(request.getParameter("status")));
			String ins = (String) request.getParameter("ins");
			action = request.getParameter("action");
			
			if ( action.trim().equalsIgnoreCase("insert") ) {
				error = fm.insertFacility(facility);
			} else if ( action.trim().equalsIgnoreCase("edit") ) {
				error = fm.updateFacility(facility);
			}
			
			if ( !error.trim().equalsIgnoreCase("") ) valid = false;
		} catch ( NumberFormatException e ) {
			valid = false;
			error = "facility.error.invalidnumbercode";
		}
		
		if ( valid ) {
			UrlUtility.redirectPage(response, "LoadFacility");
		} else {
			request.setAttribute("action", action);
			request.setAttribute("error", error);
			request.setAttribute("data", facility);
			
			UrlUtility.forwardPage(request, response, "/WEB-INF/ManageFacility.jsp");
		}
		
		
		
	}
}
